﻿#pragma once
#include <vector>
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <algorithm>

namespace Algorithm
{
    // 扑克洗牌算法
    class Shuffle
    {
        struct pocker_item
        {
            int  number;
            char color;
            pocker_item() = default;
            pocker_item(int num, char col)
            {
                number = num;
                color  = col;
            }
        };

    public:
        Shuffle();
        void                     Init();
        void                     Show();
        void                     Rand();
        std::vector<pocker_item> GetAllPocker();

    private:
        static const int         PKS_NUM    = 54;
        const int                SPAN       = 13;
        const char               colors[4]  = { 1, 2, 3, 4 };  //花色
        const char               king_small = 5;
        const char               king_big   = 6;
        std::vector<pocker_item> pks;

    public:
    };

    Shuffle::Shuffle()
        : pks(PKS_NUM)
    {
    }
    void Shuffle::Init()
    {
        int c_id = 0;
        for (int i = 0; i < PKS_NUM - 2; i++) {
            int         num = i % SPAN;
            pocker_item item;
            item.color  = colors[c_id];
            item.number = num + 1;

            if (num == 12) {
                c_id += 1;
            }
            pks[i] = item;
        }
        pks[52] = pocker_item(0, king_small);
        pks[53] = pocker_item(0, king_big);
    }
    void Shuffle::Show()
    {
        for (int i = 0; i < PKS_NUM; i++) {
            pocker_item item = pks[i];
            char        name[30];
            sprintf_s(name, "%d", item.number);
            std::string sname     = name;
            std::string colorname = "";
            if (item.color == king_small) {
                sname = "小王";
            }
            else if (item.color == king_big) {
                sname = "大王";
            }
            else if (item.color == 1) {
                colorname = "红桃";
            }
            else if (item.color == 2) {
                colorname = "梅花";
            }
            else if (item.color == 3) {
                colorname = "黑桃";
            }
            else if (item.color == 4) {
                colorname = "方块";
            }

            if (item.number == 1) {
                sname = "A";
            }
            else if (item.number == 11) {
                sname = "J";
            }
            else if (item.number == 12) {
                sname = "Q";
            }
            else if (item.number == 13) {
                sname = "K";
            }
            printf("%s%s ", colorname.c_str(), sname.c_str());
        }
    }
    void Shuffle::Rand()
    {
        srand((unsigned int)time(NULL));
        for (int i = 0; i < PKS_NUM; i++) {
            int         j    = rand() % PKS_NUM;
            pocker_item temp = pks[j];
            pks[j]           = pks[i];
            pks[i]           = temp;
        }
    }
    std::vector<Shuffle::pocker_item> Shuffle::GetAllPocker()
    {
        return pks;
    }

}  // namespace Algorithm